package com.mr.cai.main.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mr.cai.main.R;
import com.mr.cai.main.bean.CartInfoBean;
import com.mr.cai.main.utils.CommonCoreUtil;

import rx.functions.Action1;

/**
 * Created by hai8108 on 16/2/11.
 */
public class FavouriteAdapter extends MrCaiBaseAdapter {
    public FavouriteAdapter (Activity context) {
        super(context);
    }
    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(getList() == null || getList().size() <=0){
            return null;
        }
        final int index = position;
        CartInfoBean cartInfoBean = (CartInfoBean) getItem(position);
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView= View.inflate(mContext, R.layout.favourite_xlistview_item,null);
            viewHolder.cartIv = (ImageView) convertView.findViewById(R.id.cart_item_iv);

            viewHolder.cartDesc = (TextView) convertView.findViewById(R.id.cart_name);
            viewHolder.cartWeight = (TextView) convertView.findViewById(R.id.cart_weight);
            viewHolder.cartPrice = (TextView) convertView.findViewById(R.id.cart_price);
            viewHolder.addShoppingCart = (TextView) convertView.findViewById(R.id.add_to_shopping_cart);

            viewHolder.deleteIv = (ImageView) convertView.findViewById(R.id.cart_del_item);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CommonCoreUtil.setClickAction(viewHolder.deleteIv, new Action1() {
            @Override
            public void call (Object o) {
                getList().remove(index);
                notifyDataSetChanged();
            }
        });
        CommonCoreUtil.setClickAction(viewHolder.addShoppingCart, new Action1() {
            @Override
            public void call (Object o) {
                CommonCoreUtil.showToast("加入购物车");
            }
        });
        viewHolder.cartDesc.setText(cartInfoBean.getName());
        viewHolder.cartWeight.setText(cartInfoBean.getStockNo());
        viewHolder.cartPrice.setText(cartInfoBean.getPresentPrice());
        return convertView;
    }
    public static  class ViewHolder{
        private ImageView cartIv;
        private TextView cartDesc;
        private TextView cartWeight;
        private TextView cartPrice;
        private TextView addShoppingCart;
        private ImageView deleteIv;
    }
}
